package com.atguigu.crowd.funding.handler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.atguigu.crowd.funding.entity.Role;
import com.atguigu.crowd.funding.service.api.RoleService;

@Controller
public class AssignHandler {

	@Autowired
	private RoleService roleService;
	
	/**
	 * 获得该admin用户所有已分配角色和
	 * 所有没分配的角色
	 * @param adminid
	 * @return
	 */
	@RequestMapping("/assign/to/assign-role.html")
	public String getAssignRole(
			@RequestParam("adminid") Integer adminid,
			Model model ){
		System.out.println(adminid);
		//1。获得已分配角色
		List<Role> role1=roleService.getAssignRole(adminid);
		model.addAttribute("assignRole",role1 );
		//2.获得未分配的角色
		List<Role> role2=roleService.getUnAssignRole(adminid);
		model.addAttribute("unassignRole", role2);
		return "assign-role";
	}
	/**
	 * 重新分配角色
	 * （删除旧的，保存新的）
	 */
	@RequestMapping("/assign/reAssignRole.html")
	public String reAssignRole(
			@RequestParam("adminid") Integer adminid,
			@RequestParam(value="listRoleid",required=false) List<Integer> listRoleId,
			@RequestParam("pageNum") String pageNum){
		roleService.updateAssignRole(adminid,listRoleId);
		//注意：重定向需加上redirect:
		return "redirect:/admin/query/for/search.html?pageNum="+pageNum;
	}
}
